import sequelize from "../db/db";
import seq from "sequelize";
import Article from "../model/Article";
import { routerHandleReturn } from "../type";
import { Op } from "sequelize";
import { article, findtitlelimit } from "../interface";
import ArticleComment from "../model/ArticleComment";
export default {
  async getTopHeat(): Promise<Array<object> | null> {
    const result = await Article.findAll({
      order: [["browesNum", "DESC"]],
      limit: 7,
      attributes: ["title", "id"],
    });
    if (result instanceof Object) return JSON.parse(JSON.stringify(result));
    return null;
  },
  async getTopTime(): Promise<Array<object> | null> {
    const result = await Article.findAll({
      order: [["createdTime", "DESC"]],
      limit: 7,
      attributes: ["title", "id"],
    });
    if (result instanceof Object) return JSON.parse(JSON.stringify(result));
    return null;
  },
  async getTopGift(): Promise<Array<object> | null> {
    const result = await Article.findAll({
      order: [["fontlength", "DESC"]],
      limit: 7,
      attributes: ["title", "id"],
    });
    if (result instanceof Object) return JSON.parse(JSON.stringify(result));
    return null;
  },
  async getAlllimit(data: {
    x: string | number;
    y: string | number;
  }): routerHandleReturn {
    const result = await Article.findAndCountAll({
      offset: (+data.x - 1) * +data.y,
      limit: +data.y,
      distinct: true,
      include: [
        {
          model: ArticleComment,
          attributes: { exclude: ["deletedAt"] },
        },
      ],
    });
    if (result instanceof Object) return JSON.parse(JSON.stringify(result));
    return undefined;
  },
  async getFindTitle(data: {
    title: string;
    x: number;
    y: number;
  }): Promise<findtitlelimit> {
    const result = await Article.findAndCountAll({
      offset: (data.x - 1) * data.y,
      limit: data.y,
      attributes: { exclude: ["createdAt", "updatedAt", "deletedAt"] },
      where: {
        [Op.or]: {
          titles: {
            [Op.like]: `%${data.title}%`,
          },
          title: {
            [Op.like]: `%${data.title}%`,
          },
          author: {
            [Op.like]: `%${data.title}%`,
          },
          label: {
            [Op.like]: `%${data.title}%`,
          },
        },
      },
      distinct: true,
      include: [
        {
          model: ArticleComment,
          attributes: { exclude: ["deletedAt"] },
        },
      ],
    });

    return JSON.parse(
      JSON.stringify(result)
    ) as unknown as Promise<findtitlelimit>;
  },

  async add(data: object) {
    const result = await Article.create(data as any);
    return result;
  },

  async findpk(id: number | string): Promise<article | null> {
    const result = await Article.findByPk(id, {
      attributes: { exclude: ["createdAt", "updatedAt", "deletedAt"] },
      include: [
        {
          model: ArticleComment,
          attributes: { exclude: ["deletedAt"] },
        },
      ],
    });
    if (!result) {
      return null;
    }
    return result.toJSON();
  },

  async update(id: number, data: { browesNum: string }) {
    const [result] = await Article.update(data, { where: { id } });
    return result;
  },

  async Alllabel() {
    const result = await Article.findAll({
      attributes: ["label"],
    });
    return result;
  },

  //获取排序数据
  async getTimeTop(x: number, y: number, z: number) {
    console.log(1);

    const result = await Article.findAndCountAll({
      order: [["createdTime", +z > 0 ? "ASC" : "DESC"]],
      offset: (+x - 1) * +y,
      limit: +y,
      distinct: true,
      include: [
        {
          model: ArticleComment,
          attributes: { exclude: ["deletedAt"] },
        },
      ],
    });
    return result;
  },
  async getBrowesNumTop(x: number, y: number, z: number) {
    console.log(2);

    const result = await Article.findAndCountAll({
      order: [["browesNum", +z > 0 ? "ASC" : "DESC"]],
      offset: (+x - 1) * +y,
      limit: +y,
      distinct: true,
      include: [
        {
          model: ArticleComment,
          attributes: { exclude: ["deletedAt"] },
        },
      ],
    });
    return result;
  },
};
